package cs2340.quarterblack.wheresmystuff;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class SearchItem extends Activity {
	
	Spinner spinner;
	EditText etSearch;
	ListView lvResult;
	boolean hasItems = true;
	ArrayList<Item> list;
	Account account;
	ArrayAdapter<Item> adapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_search_item);
		
		spinner = (Spinner)findViewById(R.id.si_spinner);
		account = Model.getAccount();
		etSearch = (EditText)findViewById(R.id.si_search);
		
		// This will display the results
		lvResult = (ListView)findViewById(R.id.si_founditem);
		
		lvResult.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			if(hasItems == true)
			{
				Intent itemPage = new Intent(SearchItem.this,ItemPage.class);
				Model.setItem(list.get(arg2));
				SearchItem.this.startActivity(itemPage);		
			}
		}
			
		});
		list = new ArrayList<Item>();
		
		// This will add to list any results that exist in either lost or found
		if(account.getFoundItems() != null || account.getLostItems() != null){
			list.clear();
			if(account.getFoundItems() != null)
				list.addAll(account.getFoundItems());
			if(account.getLostItems() != null)
				list.addAll(account.getLostItems());
		}
		// This will tell you if the list is empty
		if(list.size() == 0){
			list.add(new Item(null, "No Items to Display"));
			hasItems = false;
		}
		adapter = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, list);
		lvResult.setAdapter(adapter);
		etSearch.addTextChangedListener(new Update(etSearch, adapter));
		
		ArrayAdapter<String> sadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Account.getSortList());
		spinner.setAdapter(sadapter);
		// listener added to spinner
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
            	changeList(etSearch.getText().toString());
            }

            public void onNothingSelected(AdapterView<?> arg0) {
               

            }
		});
	}
	
	private class Update implements TextWatcher{
		EditText text;
		ArrayAdapter<Item> adapter;
		Update(EditText text, ArrayAdapter<Item> adapter)
		{
			this.text = text;
			this.adapter = adapter;
		}
		// Will update the list as the text is changed
		@Override
		public void afterTextChanged(Editable arg0) {
			changeList(text.getText().toString());
			adapter.notifyDataSetChanged();
			
		}
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			
		}
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			adapter.notifyDataSetChanged();
			
			
		}
	}
	
	private void changeList(String str)
	{
		list.clear();
		// Spinner selection --> 0:Name, 1:Category, 2:Date
		if(spinner.getSelectedItemPosition() == 0)
		{
			etSearch.setInputType(InputType.TYPE_CLASS_TEXT);
			Model.setItemRep(0);
			// Adds all items to the list, lost and found
			list.clear();
			list.addAll(account.getFoundItemsName(str));
			list.addAll(account.getLostItemsName(str));
			adapter.notifyDataSetChanged();
		}
		else if(spinner.getSelectedItemPosition() == 1)
		{
			Model.setItemRep(1);
			etSearch.setInputType(InputType.TYPE_CLASS_TEXT);
			// In case of no inputs
			if(etSearch.getText().toString() == null || etSearch.getText().toString().length() == 0)
			{
				// Add every item from both lost and found to the list
				list.addAll(account.getFoundItemsCategory());
				list.addAll(account.getLostItemsCategory());
			}
			else
			{
				// Add items to the list by category
				list.addAll(account.getFoundItemsCategory(etSearch.getText().toString()));
				list.addAll(account.getLostItemsCategory(etSearch.getText().toString()));
			}
			adapter.notifyDataSetChanged();
		}
		else if(spinner.getSelectedItemPosition() == 2)
		{
			Model.setItemRep(2);
			etSearch.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
			
			DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
			// Again, when no input, return all items
			if(etSearch.getText().toString() == null || etSearch.getText().toString().length() == 0){
				list.addAll(account.getFoundItemsDate());
				list.addAll(account.getLostItemsDate());
			}
			// Sort items after a specified date
			else{
				Calendar sDate = new GregorianCalendar();
				try {
					sDate.setTime(format.parse(etSearch.getText().toString()));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				list.addAll(account.getFoundItemsDate(sDate));
				list.addAll(account.getLostItemsDate(sDate));
			}
			adapter.notifyDataSetChanged();
		}

	}
}